<template>
  <el-card class="middle-view">
    <template #header>
      <el-menu
        :default-active="state.activeIndex"
        mode="horizontal"
        @select="handleSelect"
        background-color="rgba(255,255,255,0)"
        text-color="#fff"
        active-text-color="#05e8fe"
      >
        <el-menu-item index="1">销售量</el-menu-item>
        <el-menu-item index="2">访问量</el-menu-item>
      </el-menu>
      <div class="right">
        <el-radio-group size="default" v-model="state.time">
          <el-radio-button label="今日">今日</el-radio-button>
          <el-radio-button label="本周">本周</el-radio-button>
          <el-radio-button label="本月">本月</el-radio-button>
          <el-radio-button label="今年">今年</el-radio-button>
        </el-radio-group>
        <!-- <el-date-picker
          v-model="state.time2"
          type="daterange"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :shortcuts="shortcuts"
        /> -->
      </div>
    </template>
    <div class="content">
      <v-chart :option="getOption()"></v-chart>
      <div class="rank-list">
        <div class="title">
          {{ state.activeIndex === "1" ? "门店销售额排名" : "门店访问量排名" }}
        </div>
        <div class="list">
          <div class="list-item" v-for="item in rankData" :key="item.no">
            <div class="no" :class="{ 'top-no': item.no <= 3 }">
              {{ item.no }}
            </div>
            <div class="name">{{ item.name }}</div>
            <div class="count">{{ item.count }}</div>
          </div>
        </div>
      </div>
    </div>
  </el-card>
</template>

<script setup lang="ts">
import { reactive, computed } from "vue";
import { useDataStore } from "@/stores/echartsData";
// 实例化仓库
const dataStore = useDataStore();
// 定义日期选择器的数据模型
interface DatePickerState {
  time: string;
  time2: string;
  activeIndex: string;
}
const state = reactive<DatePickerState>({
  time: "本周",
  time2: "",
  activeIndex: "1",
});
// const orderFullYearAxis = ref<string[]>([]);
// const orderFullYear = ref<number[]>([]);
// const userFullYearAxis = ref<string[]>([]);
// const userFullYear = ref<number[]>([]);
// const orderRank = ref<{ no: number; name: string; count: string }[]>([]);
// const userRank = ref<{ no: number; name: string; count: string }[]>([]);
// let rankData = ref<{ no: number; name: string; count: string }[]>([]);

// 选择是销售还是访问量页面
const handleSelect = (value: string) => {
  state.activeIndex = value;
};

// const shortcuts = [
//   {
//     text: "最近一周",
//     value: () => {
//       const end = new Date();
//       const start = new Date();
//       start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
//       return [start, end];
//     },
//   },
//   {
//     text: "最近一个月",
//     value: () => {
//       const end = new Date();
//       const start = new Date();
//       start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
//       return [start, end];
//     },
//   },
//   {
//     text: "最近三个月",
//     value: () => {
//       const end = new Date();
//       const start = new Date();
//       start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
//       return [start, end];
//     },
//   },
// ];

// echarts图表
const getOption = () => {
  const text = state.activeIndex === "1" ? "销售趋势" : "访问量趋势";
  return {
    title: {
      text,
      textStyle: {
        fontSize: 14,
        color: "white",
      },
      left: 10,
    },
    xAxis: {
      type: "category",
      data:
        state.activeIndex === "1"
          ? orderFullYearAxis.value
          : userFullYearAxis.value,
      // 可以保证刻度线和标签对齐
      axisTick: {
        alignWithLabel: true,
      },
      axisLabel: {
        color: "white",
        fontSize: "13px",
      },
    },
    yAxis: {
      // 坐标轴在 grid 区域中的分隔线
      splitLine: {
        show: false,
      },
      axisLabel: {
        color: "white",
        fontSize: "13px",
      },
    },
    series: [
      {
        data:
          state.activeIndex === "1" ? orderFullYear.value : userFullYear.value,
        type: "bar",
        barWidth: "35%",
        color: {
          type: "linear",
          x: 0, //右
          y: 0, //下
          x2: 0, //左
          y2: 1, //上
          colorStops: [
            {
              offset: 0,
              color: "#83bff6", // 0% 处的颜色
            },
            {
              offset: 1,
              color: "#188df0", // 100% 处的颜色
            },
          ],
        },
      },
    ],
    grid: {
      top: 50,
      left: 50,
      right: 50,
      bottom: 50,
    },
    tooltip: {},
    color: ["skyblue"], // 柱状图柱子颜色
  };
};
// 对数据进行监控
// watch(dataStore, (newVal) => {
//   orderFullYearAxis.value = newVal.reportData.orderFullYearAxis as string[];
//   orderFullYear.value = newVal.reportData.orderFullYear as number[];
//   userFullYearAxis.value = newVal.reportData.userFullYearAxis as string[];
//   userFullYear.value = newVal.reportData.userFullYear as number[];
//
//   orderRank.value = newVal.reportData.orderRank as {
//     no: number;
//     name: string;
//     count: string;
//   }[];
//   userRank.value = newVal.reportData.userRank as {
//     no: number;
//     name: string;
//     count: string;
//   }[];
//   rankData.value = state.activeIndex === "1" ? orderRank.value : userRank.value;
//
//   console.log(rankData, userRank);
// });

const orderFullYearAxis = computed(
  () => dataStore.reportData.orderFullYearAxis || []
);
const orderFullYear = computed(() => dataStore.reportData.orderFullYear || []);
const userFullYearAxis = computed(
  () => dataStore.reportData.userFullYearAxis || []
);
const userFullYear = computed(() => dataStore.reportData.userFullYear || []);
const orderRank = computed(() => dataStore.reportData.orderRank);
const userRank = computed(() => dataStore.reportData.userRank);
const rankData = computed(() =>
  state.activeIndex === "1" ? orderRank.value : userRank.value
);
</script>
<script lang="ts">
export default {
  name: "MiddleView",
};
</script>

<style lang="scss" scoped>
.middle-view {
  :deep(.el-range-input) {
    background-color: rgba(255, 255, 255, 0);
  }
  :deep(.el-card__header) {
    // display: flex;
    position: relative;
    border-bottom: none;
    padding: 0 20px;

    .right {
      position: absolute;
      top: 0;
      right: 20px;
      height: 100%;
      display: flex;
      align-items: center;
      .el-date-editor {
        margin-left: 20px;
      }
    }
  }

  .content {
    display: flex;
    height: 200px;

    .echarts {
      flex: 5;
    }

    .rank-list {
      flex: 2;

      .title {
        font-size: 17px;
        color: white;
        font-weight: 500;
        font-family: YouSheBiaoTiHei;
        color: #05e8fe;
      }

      .list {
        // margin-top: 5px;

        .list-item {
          display: flex;
          align-items: center;
          height: 20px;
          font-size: 12px;
          margin: 6px 20px 6px 0;
          font-family: YouSheBiaoTiHei;

          .no {
            width: 20px;
            height: 20px;
            color: white;
            font-size: 17px;
            display: flex;
            justify-content: center;
            align-items: center;

            &.top-no {
              border-radius: 50%;
              // color: #c35851;
              font-weight: 300;
            }
          }

          .name {
            margin-left: 10px;
            color: #fff;
          }

          .count {
            flex: 1;
            text-align: right;
            color: #fff;
          }
        }
      }
    }
  }
}
</style>
